package csoei.bbs.Dao;
import java.sql.*;
public class CategoryDao {
	static String sql="";
	static final String driver="com.mysql.jdbc.Driver";
	static final String url="jdbc:mysql://localhost:3306/csoeibbs";
	static final String db_username="root";static final String db_password="666666";
	public static ResultSet getCategories(){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select cid,catname,catintro from category order by catorder,cid desc";
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();rs=stmt.executeQuery(sql);
				return rs;
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}		
		return rs;
	}
	public static ResultSet getCidByTid(String tid){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select cid from topic where tid="+tid;
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();rs=stmt.executeQuery(sql);
				return rs;
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}		
		return rs;
	}
	public static String getCatnameBycid(String cid){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select catname from category where cid="+cid;
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				if(rs.next()){return rs.getString(1);}
				rs.close();stmt.close();conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}
		return null;
	}
}
